package com.zhsj.OkeHomeConsumption.ui.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.dialog.WaitDialog;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.common.OkeHomeConsumptionApi;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.empty.Remake;
import com.zhsj.OkeHomeConsumption.network.HttpClient;
import com.zhsj.OkeHomeConsumption.network.JsonResponseHandler;
import com.zhsj.OkeHomeConsumption.other.URLEntity;
import com.zhsj.OkeHomeConsumption.ui.activity.MineUserAuthFragment;
import com.zhsj.OkeHomeConsumption.utils.JsonUtils;
import com.zhsj.OkeHomeConsumption.utils.LogUtils;
import com.zhsj.OkeHomeConsumption.utils.SignUtil;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 *会员--我的钱包
 * */
public class WalletActivity extends MyActivity {


    @BindView(R.id.fanhuiedu_text)
    TextView fanhuieduText;
    @BindView(R.id.jifen_text)
    TextView jifenText;
    @BindView(R.id.yue_text)
    TextView yueText;
    //审核状态
    private String kyc_status;
    //钱包状态
    private String stat_user;

    private LinearLayout lin;
    private LinearLayout jifenlin;
    private FormalUserInfo userInfo;
    private String fanhuan_price ;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_huiyuanwallet;
    }

    @Override
    protected int getTitleId() {
        return R.id.title;
    }

    protected void initView() {
        userInfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        lin = findViewById(R.id.lin);
        jifenlin = findViewById(R.id.jifenlin);
        lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MinewalletActivity.class);
            }
        });
        jifenlin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(jifenText.getText().toString().equals("0")){
                    toast("暂无可兑换的积分");
                }else{
                    IsWalletUser(userInfo.getId()+"","RSA");
                }
            }
        });
    }

    @Override
    protected void initData() {
        IsWalletUser();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    //查询钱包信息
    public void IsWalletUser() {
        // post传参使用的HashMap
        HashMap<String, String> paramMaps = new HashMap<String, String>();
        // 生成sign值所使用的HashMap
        HashMap<String, String> mSendCodeHashMap = new HashMap<String, String>();
//        mSendCodeHashMap.put("token", Constants.TOKEN);
        mSendCodeHashMap.put("v", Constants.APPVERSION);
        mSendCodeHashMap.put("t", Constants.TIME);
        mSendCodeHashMap.put("deviceId", URLEntity.getInstance().getImei());
        mSendCodeHashMap.put("platform", Constants.PLAFORM);
        mSendCodeHashMap.put("deviceToken", Constants.DEVICETOKEN);
        mSendCodeHashMap.put("client", Constants.CLIENT);

        // 网络计时器及线程
//        TimeOutHandler.pDialogUtils = new WaitDialog.Builder(this)
////                .setMessage("加载中...") // 消息文本可以不用填写
////                .show();
        // 获取sign签名
        Constants.SIGN = SignUtil.getInstance().getSign(mSendCodeHashMap);
        LogUtils.e("mSendCodeHashMap", SignUtil.getInstance().getSign(mSendCodeHashMap));
        HttpClient.get(OkeHomeConsumptionApi.ELVDOU_WALLET, paramMaps, new JsonResponseHandler() {

            @Override
            public void onSuccess(int statusCode, String content) {
                // TODO Auto-generated method stub
                super.onSuccess(statusCode, content);
//                TimeOutHandler.pDialogUtils.dismiss();
//                TimeOutHandler.pDialogUtils = null;
                try {
                    JSONTokener jsonT = new JSONTokener(content);
                    JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
                    String code = jsonOBJ.optString("code");
                    String message = jsonOBJ.optString("message");
                    if (code.equals("N000000")) {
                        // 数据对接正常
                        JSONObject jsonObject = jsonOBJ.optJSONObject("data");
                        Remake  remake = JsonUtils.object(jsonObject.toString(), Remake.class);

                            fanhuieduText.setText(remake.getBackAmount() + "");
                            yueText.setText(remake.getLianLianBalance() + "");
                            jifenText.setText(remake.getIntegral() + "");
                        fanhuan_price =String.valueOf(remake.getIntegral()) ;
                    } else if (code.equals(Constants.STATUSTOKENERROR)) {
                        if (!message.equals("null")) {
                        }
                    } else {
                        if (!message.equals("null")) {
                        }
                    }
                } catch (Exception ex) {
                    LogUtils.e("DresserPartnerRegisterPost", "失败 ");
                }
            }

            @Override

            public void onFailure(Throwable e) {
                // TODO Auto-generated method stub
                super.onFailure(e);
//                TimeOutHandler.pDialogUtils.dismiss();
//                TimeOutHandler.pDialogUtils = null;
                LogUtils.e("onFailure", "" + e.toString());
            }
        });
    }
    //是否是钱包用户
    public void IsWalletUser(final String user_id, String sign_type) {
        // post传参使用的HashMap
        HashMap<String, String> paramMaps = new HashMap<String, String>();
        // 生成sign值所使用的HashMap
        HashMap<String, String> mSendCodeHashMap = new HashMap<String, String>();
//        mSendCodeHashMap.put("token", Constants.TOKEN);
        mSendCodeHashMap.put("v", Constants.APPVERSION);
        mSendCodeHashMap.put("t", Constants.TIME);
        mSendCodeHashMap.put("deviceId", URLEntity.getInstance().getImei());
        mSendCodeHashMap.put("platform", Constants.PLAFORM);
        mSendCodeHashMap.put("deviceToken", Constants.DEVICETOKEN);
        mSendCodeHashMap.put("client", Constants.CLIENT);
        mSendCodeHashMap.put("user_id", user_id);
        mSendCodeHashMap.put("sign_type", sign_type);

//        LogUtils.e("mSendCodeHashMap", mSendCodeHashMap.toString());
        //post 传参
        paramMaps.put("user_id", user_id);
        paramMaps.put("sign_type", sign_type);

        // 网络计时器及线程
        // 获取sign签名
        Constants.SIGN = SignUtil.getInstance().getSign(mSendCodeHashMap);
        LogUtils.e("mSendCodeHashMap", SignUtil.getInstance().getSign(mSendCodeHashMap));
        HttpClient.post(OkeHomeConsumptionApi.IS_WALLET_USER, paramMaps, new JsonResponseHandler() {

            @Override
            public void onSuccess(int statusCode, String content) {
                // TODO Auto-generated method stub
                super.onSuccess(statusCode, content);
                TimeOutHandler.pDialogUtils = null;
                try {
//                    LogUtils.e("content_判断是否钱包用户", "content=" + content);
                    JSONTokener jsonT = new JSONTokener(content);
                    JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
                    String code = jsonOBJ.optString("code");
                    String message = jsonOBJ.optString("message");
                    if (code.equals("N000000")) {
                        // 数据对接正常
                        JSONObject jsonObject = jsonOBJ.optJSONObject("data");
                        JSONObject singleuserquery = jsonObject.optJSONObject("singleuserquery");
                        String ret_code = singleuserquery.optString("ret_code");
                        kyc_status = singleuserquery.optString("kyc_status");
                        stat_user = singleuserquery.optString("stat_user");
                        if ("0000".equals(ret_code)) {
                            if ("1".equals(kyc_status) ||"4".equals(kyc_status) || "2".equals(stat_user)|| "1".equals(stat_user)) {
                                startActivity(new Intent(getActivity(), jifenActivity.class).putExtra("fanhuan_price", fanhuan_price));
                            } } else if ("3".equals(kyc_status)) {
                                toast("您已提交钱包开户，请耐心等待");
                            } else {
                                startActivity(MinewalletActivity.class);
                            }
                    } else if (code.equals(Constants.STATUSTOKENERROR)) {
                        if (!message.equals("null")) {
                        }
                    } else {
                        if (!message.equals("null")) {
                        }
                    }
                } catch (Exception ex) {
                    LogUtils.e("DresserPartnerRegisterPost", "失败 ");
                }
            }

            @Override

            public void onFailure(Throwable e) {
                // TODO Auto-generated method stub
                super.onFailure(e);
                TimeOutHandler.pDialogUtils = null;
                LogUtils.e("onFailure", "" + e.toString());
            }
        });
    }
}
